﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using AnimalCare.Controller;
using AnimalCare.Service;
using AnimalCare.Model;

namespace AnimalCare.View
{
    public partial class FormSale : DevExpress.XtraEditors.XtraForm
    {
        protected List<ProductDetail> orderDetail = new List<ProductDetail>();
        double total;
        List<ProductDetail> products;
        public FormSale()
        {
            InitializeComponent();
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            customerGridControl.DataSource = CustomerService.search(textEdit1.Text);
            customerGridControl.Refresh();
        }

        private void FormSale_Load(object sender, EventArgs e)
        {
            dePay.DateTime = DateTime.Now;
            txtNumber.Text = OrderService.count().ToString();
            products = ProductService.search(txtProductName.Text);
            customerGridControl.DataSource = CustomerService.search(textEdit1.Text);
            customerGridControl.Refresh();
            productGridControl.DataSource = products;
            productGridControl.Refresh();
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (productView.RowCount > 0)
            {
                bool isHave = false;
                bool isOnSale;
                total = 0;
                int amount = Convert.ToInt32(txtAmount.Text);
                ProductDetail currentProduct = new ProductDetail();
                int[] selectedRowsIndex = productView.GetSelectedRows();
                if (selectedRowsIndex.Length > 0)
                {
                    int indexRow = selectedRowsIndex[0];
                    currentProduct = (ProductDetail)productView.GetRow(indexRow);
                }
                if (currentProduct.total == 0)
                {
                    isOnSale = false;
                }
                else
                {
                    isOnSale = true;
                }
                if (currentProduct.total < amount)
                {
                    amount = currentProduct.total.Value;
                }
                currentProduct.total = currentProduct.total - amount;

                List<ProductDetail> tempList = new List<ProductDetail>();
                foreach (ProductDetail pd in orderDetail)
                {
                    ProductDetail newPd = ProductService.copyProductDetail(pd);
                    if (newPd.product_id == currentProduct.product_id && isOnSale)
                    {
                        newPd.total = newPd.total + amount;
                        isHave = true;
                    }
                    tempList.Add(newPd);
                    total = total + (newPd.price.Value * newPd.total.Value);
                }
                if (!isHave && isOnSale)
                {
                    ProductDetail newPd = ProductService.copyProductDetail(currentProduct);
                    newPd.total = amount;
                    tempList.Add(newPd);
                    total = total + (newPd.price.Value * newPd.total.Value);
                }
                lbTotal.Text = String.Format("{0}VNĐ", total);
                orderDetail = tempList;
                List<ProductDetail> productList = refeshProductView();
                productGridControl.DataSource = productList;
                productGridControl.Refresh();
                OrderGridControl.DataSource = orderDetail;
                OrderGridControl.Refresh();
                if (total == 0)
                {
                    btPay.Enabled = false;
                }
                else
                {
                    btPay.Enabled = true;
                }
            }
        }

        private void btNewCustomer_Click(object sender, EventArgs e)
        {
            FormCustomer fc = new FormCustomer();
            fc.ShowDialog();
            customerGridControl.DataSource = CustomerService.search(textEdit1.Text);
        }

        private void bỏRaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (orderView.RowCount > 0)
            {
                total = 0;
                ProductDetail currentProduct = new ProductDetail();
                int[] selectedRowsIndex = orderView.GetSelectedRows();
                if (selectedRowsIndex.Length > 0)
                {
                    int indexRow = selectedRowsIndex[0];
                    currentProduct = (ProductDetail)orderView.GetRow(indexRow);
                }
                foreach (ProductDetail pd in products)
                {
                    if (currentProduct.product_id == pd.product_id)
                    {
                        pd.total = currentProduct.total + pd.total;
                    }
                }
                List<ProductDetail> tempList = new List<ProductDetail>();
                foreach (ProductDetail pd in orderDetail)
                {
                    ProductDetail newPd = ProductService.copyProductDetail(pd);
                    if (newPd.product_id != currentProduct.product_id)
                    {
                        tempList.Add(newPd);
                        total = total + (newPd.price.Value * newPd.total.Value);
                    }
                }
                lbTotal.Text = String.Format("{0}VNĐ", total);
                orderDetail = tempList;
                List<ProductDetail> productList = refeshProductView();
                productGridControl.DataSource = productList;
                productGridControl.Refresh();
                OrderGridControl.DataSource = orderDetail;
                OrderGridControl.Refresh();
                if (total == 0)
                {
                    btPay.Enabled = false;
                }
                else
                {
                    btPay.Enabled = true;
                }
            }
        }

        private void txtProductName_EditValueChanged(object sender, EventArgs e)
        {
            List<ProductDetail> productList = refeshProductView();
            productGridControl.DataSource = productList;
            productGridControl.Refresh();

            //productGridControl.DataSource = ProductService.search(txtProductName.Text);
            //productGridControl.Refresh();
        }
        private List<ProductDetail> refeshProductView()
        {
            List<ProductDetail> productList = new List<ProductDetail>();
            foreach (ProductDetail pd in products)
            {
                if (pd.product_name.ToLower().Contains(txtProductName.Text.ToLower()))
                {
                    productList.Add(pd);
                }
            }
            return productList;
        }

        private void btPay_Click(object sender, EventArgs e)
        {
            txtPay.Text = total.ToString();
            Customer currentCustomer = new Customer();
            int[] selectedRowsIndex = orderView.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                currentCustomer = (Customer)customerView.GetRow(indexRow);
            }
            OrderMain order = new OrderMain();
            order.oder_number = txtNumber.Text;
            order.order_day_date = DateTime.Now;
            order.payment = Convert.ToDouble(txtPay.Text);
            order.debt = total - order.payment;
            order.payment_day_date = dePay.DateTime;
            order.order_info = txtNote.Text;
            OrderService.Insert(order);

            foreach (ProductDetail pd in orderDetail)
            {
                Oder_Item oi = new Oder_Item();
                oi.oder_id = OrderService.getOrderId(order.oder_number);
                oi.product_id = pd.product_id;
                oi.total = pd.total;
                OderItemService.Insert(oi);
            }

            foreach (ProductDetail pd in products)
            {
                Product p = ProductService.copyProductDetailToProduct(pd);
                ProductService.UpdateTotal(p);
            }

            MessageBox.Show(String.Format("Thanh toán thành công : Đã trả {0} VNĐ còn lại {1} VNĐ !", order.payment.ToString(), order.debt.ToString())
            , "Thanh toán", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btPay.Enabled = false;
            addProduct.Enabled = false;
            removeProduct.Enabled = false;
        }
    }
}
